from pydantic import BaseModel, Field, validator
from typing import List, Optional, Literal
from datetime import datetime

class Message(BaseModel):
    """聊天消息模型"""
    role: Literal["system", "user", "assistant"]
    content: str = Field(..., min_length=1)

class ChatRequest(BaseModel):
    """聊天请求模型"""
    input_text: str = Field(..., min_length=1, description="用户输入文本")
    temperature: float = Field(0.7, ge=0.0, le=2.0)
    max_tokens: int = Field(1000, ge=1, le=4096)
    top_p: float = Field(0.95, ge=0.0, le=1.0)
    frequency_penalty: float = Field(0.0, ge=-2.0, le=2.0)
    presence_penalty: float = Field(0.0, ge=-2.0, le=2.0)

    @validator("input_text")
    def validate_input_text(cls, v):
        if not v.strip():
            raise ValueError("输入文本不能为空")
        return v.strip()

class PromptRequest(BaseModel):
    """提示词生成请求模型"""
    input_text: str = Field(..., min_length=1, description="原始中文描述")
    prompt_type: Literal["normal", "master"] = Field("normal", description="提示词类型")
    temperature: float = Field(0.7, ge=0.0, le=2.0)
    max_tokens: int = Field(1000, ge=1, le=4096)

    @validator("input_text")
    def validate_input_text(cls, v):
        if not v.strip():
            raise ValueError("输入文本不能为空")
        return v.strip()

class ResponseData(BaseModel):
    """响应数据模型"""
    output_text: str = Field(..., description="输出文本")
    parameters: dict = Field(..., description="请求参数")

class ApiResponse(BaseModel):
    """API响应基础模型"""
    code: int = Field(200, description="状态码")
    message: str = Field("success", description="状态信息")
    data: Optional[ResponseData] = Field(None, description="响应数据")
    timestamp: datetime = Field(default_factory=datetime.utcnow)

class ErrorResponse(BaseModel):
    """错误响应模型"""
    code: int
    message: str
    details: Optional[dict] = None
    timestamp: datetime = Field(default_factory=datetime.utcnow)

class PromptResponse(ApiResponse):
    """提示词生成响应模型"""
    data: ResponseData = Field(..., description="响应数据，包含生成的提示词")

class ChatResponse(ApiResponse):
    """聊天响应模型"""
    data: ResponseData = Field(..., description="响应数据，包含聊天回复") 